package net.dopan.pigframe.user.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.dopan.pigframe.core.base.controller.BaseController;
import net.dopan.pigframe.core.base.controller.response.ResponseBean;
import net.dopan.pigframe.core.utils.CommonConstant;
import net.dopan.pigframe.core.utils.Query;
import net.dopan.pigframe.user.model.dto.RoleDTO;
import net.dopan.pigframe.user.model.entity.SysRole;
import net.dopan.pigframe.user.service.SysRoleMenuService;
import net.dopan.pigframe.user.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Package: net.dopan.pigframe.user.controller
 * @Title: RoleController
 * @Description: 角色
 * @author: 刘宽
 * @date: 2018/9/2 21:24
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色接口(API)", description = "角色相关操作")
public class RoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 通过ID查询角色信息
     *
     * @param id ID
     * @return 角色信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询角色信息", notes = "通过ID查询角色信息")
    public SysRole role(@PathVariable Integer id) {
        return sysRoleService.selectById(id);
    }

    /**
     * 添加角色
     *
     * @param roleDto 角色信息
     * @return success、false
     */
    @PostMapping
    @ApiOperation(value = "添加角色", notes = "添加角色")
    public ResponseBean<Boolean> role(@RequestBody RoleDTO roleDto) {
        return new ResponseBean<>(sysRoleService.insertRole(roleDto));
    }

    /**
     * 修改角色
     *
     * @param roleDto 角色信息
     * @return success/false
     */
    @PutMapping
    @ApiOperation(value = "修改角色", notes = "修改角色")
    public ResponseBean<Boolean> roleUpdate(@RequestBody RoleDTO roleDto) {
        return new ResponseBean<>(sysRoleService.updateRoleById(roleDto));
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除角色", notes = "删除角色")
    public ResponseBean<Boolean> roleDel(@PathVariable Integer id) {
        SysRole sysRole = sysRoleService.selectById(id);
        sysRole.setDelFlag(CommonConstant.STATUS_DEL);
        return new ResponseBean<>(sysRoleService.updateById(sysRole));
    }

    /**
     * 获取角色列表
     *
     * @param deptId 部门ID
     * @return 角色列表
     */
    @GetMapping("/roleList/{deptId}")
    @ApiOperation(value = "获取角色列表", notes = "获取角色列表")
    public List<SysRole> roleList(@PathVariable Integer deptId) {
        return sysRoleService.selectListByDeptId(deptId);

    }

    /**
     * 分页查询角色信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/rolePage")
    @ApiOperation(value = "分页查询角色信息", notes = "分页查询角色信息")
    public Page rolePage(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return sysRoleService.selectwithDeptPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 更新角色菜单
     *
     * @param roleId  角色ID
     * @param menuIds 菜单结合
     * @return success、false
     */
    @PutMapping("/roleMenuUpd")
    @ApiOperation(value = "更新角色菜单", notes = "更新角色菜单")
    public ResponseBean<Boolean> roleMenuUpd(Integer roleId, @RequestParam(value = "menuIds", required = false) String menuIds) {
        SysRole sysRole = sysRoleService.selectById(roleId);
        return new ResponseBean<>(sysRoleMenuService.insertRoleMenus(sysRole.getRoleCode(), roleId, menuIds));
    }
}
